API - Database\CotDB
system\database.php at line 25

Class CotDB

PDO
└─CotDB

public class CotDB
extends PDO

Cotonti Database Connection class. A compact extension to standard PHP PDO class with slight Cotonti-specific needs, handy functions and query builder.
See Also:
http://www.php.net/manual/en/class.pdo.php
Property-read:
int $affectedRows Number of rows affected by the most recent query
int $count Total query count
int $timeCount Total query execution time

Field Summary
private int

$_affected_rows

Number of rows affected by the most recent query

private int

$_count

Total query count

private bool

$_prepare_itself

Prepare statements by itself.

private array

$_tables

Table names registry

private int

$_tcount

Total query execution time

private string

$_xtime

Timer start microtime

Constructor Summary
void

__construct(string dsn, string username, string passwd, array options)

Creates a PDO instance to represent a connection to the requested database.

Method Summary
mixed

__get(string name)

Provides access to properties

private void

_bindParams(PDOStatement statement, array parameters)

Binds parameters to a statement

private bool

_parseError(PDOException e, string err_code, string err_message)

Parses PDO exception message and returns its components and status

private string

_prepare(string query, array parameters)

Prepares a parametrized query on client side

private void

_startTimer()

Starts query execution timer

private void

_stopTimer(mixed query)

Stops query execution timer

int

countRows(string table_name)

Returns total number of records contained in a table

int

delete(string table_name, string condition, array parameters)

Performs simple SQL DELETE query and returns number of removed items.

bool

fieldExists(string table_name, string field_name)

Checks if a field exists in a table

bool

indexExists(string table_name, string index_name, mixed index_columns)

Checks if an index with the same index name or column order exists

int

addIndex(string table_name, string index_name, mixed Either, mixed index_columns)

Adds an index on a table

int

insert(string table_name, array data, bool insert_null, bool ignore, array update_fields)

Performs SQL INSERT on simple data array.

string

prep(string str)

Prepares a param for use in SQL query without wrapping it with quotes

void

registerTable(string table_name)

Registers an unprefixed table name in table names registry

string

runScript(string script, resource conn)

Runs an SQL script containing multiple queries.

PDOStatement

query(string query, array parameters)

1) If called with one parameter: Works like PDO::query() Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object.

int

update(string table_name, array data, string condition, array parameters, bool update_null)

Performs SQL UPDATE with simple data array.

Field Detail

system\database.php at line 30

_affected_rows

private int $_affected_rows = 0
Number of rows affected by the most recent query

system\database.php at line 36

_count

private int $_count = 0
Total query count

system\database.php at line 42

_prepare_itself

private bool $_prepare_itself = false
Prepare statements by itself. Used with MySQL client API versions prior to 5.1

system\database.php at line 60

_tables

private array $_tables = array()
Table names registry

system\database.php at line 48

_tcount

private int $_tcount = 0
Total query execution time

system\database.php at line 54

_xtime

private string $_xtime = 0
Timer start microtime

Constructor Detail

system\database.php at line 71

__construct

public void __construct(string dsn, string username, string passwd, array options)
Creates a PDO instance to represent a connection to the requested database.
Parameters:
dsn - The Data Source Name, or DSN, contains the information required to connect to the database.
username - The user name for the DSN string.
passwd - The password for the DSN string.
options - A key=>value array of driver-specific connection options.
See Also:
http://www.php.net/manual/en/pdo.construct.php

Method Detail

system\database.php at line 97

__get

public mixed __get(string name)
Provides access to properties
Parameters:
name - Property name
Returns:
Property value

system\database.php at line 121

_bindParams

private void _bindParams(PDOStatement statement, array parameters)
Binds parameters to a statement
Parameters:
statement - PDO statement
parameters - Array of parameters, numeric or associative

system\database.php at line 139

_parseError

private bool _parseError(PDOException e, string err_code, string err_message)
Parses PDO exception message and returns its components and status
Parameters:
e - PDO Exception
err_code - Output error code parameter
err_message - Output error message parameter
Returns:
TRUE for error cases, FALSE for notifications and warnings

system\database.php at line 162

_prepare

private string _prepare(string query, array parameters)
Prepares a parametrized query on client side
Parameters:
query - Query being prepared
parameters - Associative or numeric array of parameters
Returns:
Array with placeholders substituted

system\database.php at line 183

_startTimer

private void _startTimer()
Starts query execution timer

system\database.php at line 196

_stopTimer

private void _stopTimer(mixed query)
Stops query execution timer

system\database.php at line 225

countRows

public int countRows(string table_name)
Returns total number of records contained in a table
Parameters:
table_name - Table name
Returns:

system\database.php at line 238

delete

public int delete(string table_name, string condition, array parameters)
Performs simple SQL DELETE query and returns number of removed items.
Parameters:
table_name - Table name
condition - Body of WHERE clause
parameters - Array of statement input parameters, see http://www.php.net/manual/en/pdostatement.execute.php
Returns:
Number of records removed on success or FALSE on error

system\database.php at line 285

fieldExists

public bool fieldExists(string table_name, string field_name)
Checks if a field exists in a table
Parameters:
table_name - Table name
field_name - Field name
Returns:
TRUE if the field exists, FALSE otherwise

system\database.php at line 298

indexExists

public bool indexExists(string table_name, string index_name, mixed index_columns)
Checks if an index with the same index name or column order exists
Parameters:
table_name - Table name
index_name - Index/Key name
index_columns - Either a string for a single column name or an array for single/multiple columns. No column check will be preformed if left empty.
Returns:
TRUE if the index name or column order exists, FALSE otherwise

system\database.php at line 339

addIndex

public int addIndex(string table_name, string index_name, mixed Either, mixed index_columns)
Adds an index on a table
Parameters:
table_name - Table name
index_name - Index/Key name
Either - a string for a single column name or an array for single/multiple columns. $index_name will be used if empty.
Returns:
Number of rows affected

system\database.php at line 369

insert

public int insert(string table_name, array data, bool insert_null, bool ignore, array update_fields)
Performs SQL INSERT on simple data array. Array keys must match table keys, optionally you can specify key prefix as third parameter. Strings get quoted and escaped automatically. Ints and floats must be typecasted. You can use special values in the array: - PHP NULL => SQL NULL - 'NOW()' => SQL NOW() Performs single row INSERT if $data is an associative array, performs multi-row INSERT if $data is a 2D array (numeric => assoc)
Parameters:
table_name - Table name
data - Associative or 2D array containing data for insertion.
insert_null - Insert SQL NULL for empty values rather than ignoring them.
ignore - Ignore duplicate key errors on insert
update_fields - List of fields to be updated with ON DUPLICATE KEY UPDATE
Returns:
The number of affected records

system\database.php at line 473

prep

public string prep(string str)
Prepares a param for use in SQL query without wrapping it with quotes
Parameters:
str - Param string
Returns:
Escaped param

system\database.php at line 482

registerTable

public void registerTable(string table_name)
Registers an unprefixed table name in table names registry
Parameters:
table_name - Table name without a prefix, e.g. 'pages'

system\database.php at line 498

runScript

public string runScript(string script, resource conn)
Runs an SQL script containing multiple queries.
Parameters:
script - SQL script body, containing formatted queries separated by semicolons and newlines
conn - Custom connection handle
Returns:
Error message if an error occurs or empty string on success

system\database.php at line 551

query

public PDOStatement query(string query, array parameters)
1) If called with one parameter: Works like PDO::query() Executes an SQL statement in a single function call, returning the result set (if any) returned by the statement as a PDOStatement object. 2) If called with second parameter as array of input parameter bindings: Works like PDO::prepare()->execute() Prepares an SQL statement and executes it.
See Also:
http://www.php.net/manual/en/pdo.query.php
http://www.php.net/manual/en/pdo.prepare.php
Parameters:
query - The SQL statement to prepare and execute.
parameters - An array of values to be binded as input parameters to the query. PHP int parameters will beconsidered as PDO::PARAM_INT, others as PDO::PARAM_STR.
Returns:

system\database.php at line 607

update

public int update(string table_name, array data, string condition, array parameters, bool update_null)
Performs SQL UPDATE with simple data array. Array keys must match table keys, optionally you can specify key prefix as fourth parameter. Strings get quoted and escaped automatically. Ints and floats must be typecasted. You can use special values in the array: - PHP NULL => SQL NULL - 'NOW()' => SQL NOW()
Parameters:
table_name - Table name
data - Associative array containing data for update
condition - Body of SQL WHERE clause
parameters - Array of statement input parameters, see http://www.php.net/manual/en/pdostatement.execute.php
update_null - Nullify cells which have null values in the array. By default they are skipped
Returns:
The number of affected records or FALSE on error